﻿
Option Strict Off
Option Explicit On

Imports CupsApp.Data
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
Imports System.Data.Linq
Imports System.Linq
Imports System.Web.DomainServices
Imports System.Web

Imports System.Web.Ria
Imports System.Web.Ria.Data
Imports System.Web.DomainServices.Providers
Imports System.Data.SqlTypes
Imports System.Web.Security


'Implements application logic using the CupsDataContext context.
' TODO: Add your application logic to these methods or in additional methods.
<EnableClientAccess()> _
Public Class CupsDomainService
    Inherits LinqToSqlDomainService(Of CupsDataContext)
    Implements IClientDomainService
    Implements IInterviewDomainService





    Public Sub New()
        Dim ctx As New CupsDataContext()
        ctx.Log = DebugTextWriter.Null
        _clientRepo = New LinqToSqlRepository(Of cups_Client)(ctx)
        _interviewTemplateRepo = New LinqToSqlRepository(Of cups_InterviewTemplate)(ctx)
        _interviewTemplateQuestionRepo = New LinqToSqlRepository(Of cups_InterviewTemplateQuestion)(ctx)
        _interviewRepo = New LinqToSqlRepository(Of cups_Interview)(ctx)
        _interviewQuestionRepo = New LinqToSqlRepository(Of cups_InterviewQuestion)(ctx)
        _statusRepo = New LinqToSqlRepository(Of cups_ClientStatus)(ctx)
        _controlMappingRepo = New LinqToSqlRepository(Of cups_ControlMapping)(ctx)
        _repoLandlord = New LinqToSqlRepository(Of cups_Landlord)(ctx)
        _repoProperty = New LinqToSqlRepository(Of cups_PropertyLandlord)(ctx)
        _clientStatusUpdateTypeRepo = New LinqToSqlRepository(Of cups_StatusUpdateType)(ctx)
        _clientPropertyLandlordRepo = New LinqToSqlRepository(Of cups_ClientPropertyLandlord)(ctx)
        _PropertyLandlordStatus = New LinqToSqlRepository(Of cups_PropertyLandlordStatus)(ctx)

    End Sub




    Public Sub New(ByVal clientRepo As IRepository(Of cups_Client), _
                   ByVal interviewTemplateRepo As IRepository(Of cups_InterviewTemplate), _
                    ByVal interviewTemplateQuestionRepo As IRepository(Of cups_InterviewTemplateQuestion), _
                     ByVal interviewRepo As IRepository(Of cups_Interview), _
                  ByVal interviewQuestionRepo As IRepository(Of cups_InterviewQuestion))

        _clientRepo = clientRepo
        _interviewTemplateRepo = interviewTemplateRepo
        _interviewTemplateQuestionRepo = interviewTemplateQuestionRepo
        _interviewRepo = interviewRepo
        _interviewQuestionRepo = interviewQuestionRepo



    End Sub

    Public Sub New(ByVal clientRepo As IRepository(Of cups_Client), _
                   ByVal interviewTemplateRepo As IRepository(Of cups_InterviewTemplate), _
                    ByVal interviewTemplateQuestionRepo As IRepository(Of cups_InterviewTemplateQuestion), _
                     ByVal interviewRepo As IRepository(Of cups_Interview), _
                  ByVal interviewQuestionRepo As IRepository(Of cups_InterviewQuestion), _
                  ByVal statusRepo As IRepository(Of cups_ClientStatus))
        Me.New(clientRepo, interviewTemplateRepo, interviewTemplateQuestionRepo, interviewRepo, interviewQuestionRepo)
        _statusRepo = statusRepo

    End Sub

    Public Sub New(ByVal clientRepo As IRepository(Of cups_Client), _
                   ByVal interviewTemplateRepo As IRepository(Of cups_InterviewTemplate), _
                    ByVal interviewTemplateQuestionRepo As IRepository(Of cups_InterviewTemplateQuestion), _
                   ByVal interviewRepo As IRepository(Of cups_Interview), _
                  ByVal interviewQuestionRepo As IRepository(Of cups_InterviewQuestion), _
                  ByVal statusRepo As IRepository(Of cups_ClientStatus), _
                  ByVal controlMappingRepo As IRepository(Of cups_ControlMapping), _
                  ByVal LandlordRepo As IRepository(Of cups_Landlord), _
                   ByVal PropertyRepo As IRepository(Of cups_PropertyLandlord), _
                   ByVal csUpdateTypeRepo As IRepository(Of cups_StatusUpdateType), _
                   ByVal clientPropertyLandlordRepo As IRepository(Of cups_ClientPropertyLandlord), _
                    ByVal propertyLandlordStatusRepo As IRepository(Of cups_PropertyLandlordStatus))

        Me.New(clientRepo, interviewTemplateRepo, interviewTemplateQuestionRepo, interviewRepo, interviewQuestionRepo, statusRepo)
        _controlMappingRepo = controlMappingRepo
        _repoLandlord = LandlordRepo
        _repoProperty = PropertyRepo
        _clientStatusUpdateTypeRepo = csUpdateTypeRepo
        _clientPropertyLandlordRepo = clientPropertyLandlordRepo
        _PropertyLandlordStatus = PropertyLandlordStatusRepo
    End Sub




    Private _clientRepo As IRepository(Of cups_Client)
    Private _interviewTemplateRepo As IRepository(Of cups_InterviewTemplate)
    Private _interviewTemplateQuestionRepo As IRepository(Of cups_InterviewTemplateQuestion)
    Private _interviewRepo As IRepository(Of cups_Interview)
    Private _interviewQuestionRepo As IRepository(Of cups_InterviewQuestion)
    Private _statusRepo As IRepository(Of cups_ClientStatus)
    Private _controlMappingRepo As IRepository(Of cups_ControlMapping)
    Private _repoLandlord As IRepository(Of cups_Landlord)
    Private _repoProperty As IRepository(Of cups_PropertyLandlord)
    Private _clientStatusUpdateTypeRepo As IRepository(Of cups_StatusUpdateType)
    Private _clientPropertyLandlordRepo As IRepository(Of cups_ClientPropertyLandlord)
    Private _PropertyLandlordStatus As IRepository(Of cups_PropertyLandlordStatus)
    'TODO: Consider
    ' 1. Adding parameters to this method and constraining returned results, and/or
    ' 2. Adding query methods taking different parameters.
    <RequiresAuthentication()> _
    Public Overridable Function GetCups_Clients() As IQueryable(Of cups_Client)
        'Return Me.DataContext.cups_Clients

        'DataLoadOptions loadOpts = new DataLoadOptions(); loadOpts.LoadWith<Product>(p => p.ProductSubcategory); this.Context.LoadOptions = loadOpts;

        Return _clientRepo.Query()

    End Function

    Public Overridable Sub InsertCups_Client(ByVal cups_Client As cups_Client)
        ' Me.DataContext.cups_Clients.InsertOnSubmit(cups_Client)
        _clientRepo.Add(cups_Client)
        _clientRepo.SubmitChanges()

    End Sub

    Public Overridable Sub UpdateCups_Client(ByVal currentcups_Client As cups_Client)
        _clientRepo.Attach(currentcups_Client, Me.ChangeSet.GetOriginal(currentcups_Client))
    End Sub

    Public Overridable Sub DeleteCups_Client(ByVal cups_Client As cups_Client)
        Me.DataContext.cups_Clients.Attach(cups_Client)
        Me.DataContext.cups_Clients.DeleteOnSubmit(cups_Client)
    End Sub

    'TODO: Consider
    ' 1. Adding parameters to this method and constraining returned results, and/or
    ' 2. Adding query methods taking different parameters.
    Public Overridable Function GetCups_ClientPropertyLandlords() As IQueryable(Of cups_ClientPropertyLandlord)
        Return Me.DataContext.cups_ClientPropertyLandlords
    End Function

    Public Overridable Sub InsertCups_ClientPropertyLandlord(ByVal cups_ClientPropertyLandlord As cups_ClientPropertyLandlord)
        Me.DataContext.cups_ClientPropertyLandlords.InsertOnSubmit(cups_ClientPropertyLandlord)
    End Sub

    Public Overridable Sub UpdateCups_ClientPropertyLandlord(ByVal currentcups_ClientPropertyLandlord As cups_ClientPropertyLandlord)
        Me.DataContext.cups_ClientPropertyLandlords.Attach(currentcups_ClientPropertyLandlord, Me.ChangeSet.GetOriginal(currentcups_ClientPropertyLandlord))
    End Sub

    Public Overridable Sub DeleteCups_ClientPropertyLandlord(ByVal cups_ClientPropertyLandlord As cups_ClientPropertyLandlord)
        Me.DataContext.cups_ClientPropertyLandlords.Attach(cups_ClientPropertyLandlord)
        Me.DataContext.cups_ClientPropertyLandlords.DeleteOnSubmit(cups_ClientPropertyLandlord)
    End Sub

    'TODO: Consider
    ' 1. Adding parameters to this method and constraining returned results, and/or
    ' 2. Adding query methods taking different parameters.
    Public Overridable Function GetCups_ClientStatus() As IQueryable(Of cups_ClientStatus)
        Return Me.DataContext.cups_ClientStatus
    End Function

    Public Overridable Sub InsertCups_ClientStatus(ByVal cups_ClientStatus As cups_ClientStatus)
        Me.DataContext.cups_ClientStatus.InsertOnSubmit(cups_ClientStatus)
    End Sub

    Public Overridable Sub UpdateCups_ClientStatus(ByVal currentcups_ClientStatus As cups_ClientStatus)
        Me.DataContext.cups_ClientStatus.Attach(currentcups_ClientStatus, Me.ChangeSet.GetOriginal(currentcups_ClientStatus))
    End Sub

    Public Overridable Sub DeleteCups_ClientStatus(ByVal cups_ClientStatus As cups_ClientStatus)
        Me.DataContext.cups_ClientStatus.Attach(cups_ClientStatus)
        Me.DataContext.cups_ClientStatus.DeleteOnSubmit(cups_ClientStatus)
    End Sub

    'TODO: Consider
    ' 1. Adding parameters to this method and constraining returned results, and/or
    ' 2. Adding query methods taking different parameters.
    Public Overridable Function GetCups_Interviews() As IQueryable(Of cups_Interview)
        Return Me.DataContext.cups_Interviews
    End Function

    Public Overridable Sub InsertCups_Interview(ByVal cups_Interview As cups_Interview)
        Me.DataContext.cups_Interviews.InsertOnSubmit(cups_Interview)
    End Sub

    Public Overridable Sub UpdateCups_Interview(ByVal currentcups_Interview As cups_Interview)
        Me.DataContext.cups_Interviews.Attach(currentcups_Interview, Me.ChangeSet.GetOriginal(currentcups_Interview))
    End Sub

    Public Overridable Sub DeleteCups_Interview(ByVal cups_Interview As cups_Interview)
        Me.DataContext.cups_Interviews.Attach(cups_Interview)
        Me.DataContext.cups_Interviews.DeleteOnSubmit(cups_Interview)
    End Sub

    'TODO: Consider
    ' 1. Adding parameters to this method and constraining returned results, and/or
    ' 2. Adding query methods taking different parameters.
    Public Overridable Function GetCups_InterviewQuestions() As IQueryable(Of cups_InterviewQuestion)
        Return Me.DataContext.cups_InterviewQuestions
    End Function

    Public Overridable Sub InsertCups_InterviewQuestion(ByVal cups_InterviewQuestion As cups_InterviewQuestion)
        Me.DataContext.cups_InterviewQuestions.InsertOnSubmit(cups_InterviewQuestion)
    End Sub

    Public Overridable Sub UpdateCups_InterviewQuestion(ByVal currentcups_InterviewQuestion As cups_InterviewQuestion)
        Me.DataContext.cups_InterviewQuestions.Attach(currentcups_InterviewQuestion, Me.ChangeSet.GetOriginal(currentcups_InterviewQuestion))
    End Sub

    Public Overridable Sub DeleteCups_InterviewQuestion(ByVal cups_InterviewQuestion As cups_InterviewQuestion)
        Me.DataContext.cups_InterviewQuestions.Attach(cups_InterviewQuestion)
        Me.DataContext.cups_InterviewQuestions.DeleteOnSubmit(cups_InterviewQuestion)
    End Sub

    'TODO: Consider
    ' 1. Adding parameters to this method and constraining returned results, and/or
    ' 2. Adding query methods taking different parameters.
    Public Overridable Function GetCups_InterviewTemplates() As IQueryable(Of cups_InterviewTemplate)
        Return Me.DataContext.cups_InterviewTemplates
    End Function

    Public Overridable Sub InsertCups_InterviewTemplate(ByVal cups_InterviewTemplate As cups_InterviewTemplate)
        Me.DataContext.cups_InterviewTemplates.InsertOnSubmit(cups_InterviewTemplate)
    End Sub

    Public Overridable Sub UpdateCups_InterviewTemplate(ByVal currentcups_InterviewTemplate As cups_InterviewTemplate)
        Me.DataContext.cups_InterviewTemplates.Attach(currentcups_InterviewTemplate, Me.ChangeSet.GetOriginal(currentcups_InterviewTemplate))
    End Sub

    Public Overridable Sub DeleteCups_InterviewTemplate(ByVal cups_InterviewTemplate As cups_InterviewTemplate)
        Me.DataContext.cups_InterviewTemplates.Attach(cups_InterviewTemplate)
        Me.DataContext.cups_InterviewTemplates.DeleteOnSubmit(cups_InterviewTemplate)
    End Sub

    'TODO: Consider
    ' 1. Adding parameters to this method and constraining returned results, and/or
    ' 2. Adding query methods taking different parameters.
    Public Overridable Function GetCups_InterviewTemplateQuestions() As IQueryable(Of cups_InterviewTemplateQuestion)
        Return Me.DataContext.cups_InterviewTemplateQuestions
    End Function

    Public Overridable Sub InsertCups_InterviewTemplateQuestion(ByVal cups_InterviewTemplateQuestion As cups_InterviewTemplateQuestion)
        Me.DataContext.cups_InterviewTemplateQuestions.InsertOnSubmit(cups_InterviewTemplateQuestion)
    End Sub

    Public Overridable Sub UpdateCups_InterviewTemplateQuestion(ByVal currentcups_InterviewTemplateQuestion As cups_InterviewTemplateQuestion)
        Me.DataContext.cups_InterviewTemplateQuestions.Attach(currentcups_InterviewTemplateQuestion, Me.ChangeSet.GetOriginal(currentcups_InterviewTemplateQuestion))
    End Sub

    Public Overridable Sub DeleteCups_InterviewTemplateQuestion(ByVal cups_InterviewTemplateQuestion As cups_InterviewTemplateQuestion)
        Me.DataContext.cups_InterviewTemplateQuestions.Attach(cups_InterviewTemplateQuestion)
        Me.DataContext.cups_InterviewTemplateQuestions.DeleteOnSubmit(cups_InterviewTemplateQuestion)
    End Sub

    'TODO: Consider
    ' 1. Adding parameters to this method and constraining returned results, and/or
    ' 2. Adding query methods taking different parameters.
    Public Overridable Function GetCups_Landlords() As IQueryable(Of cups_Landlord)
        Return Me.DataContext.cups_Landlords
    End Function

    Public Overridable Sub InsertCups_Landlord(ByVal cups_Landlord As cups_Landlord)
        Me.DataContext.cups_Landlords.InsertOnSubmit(cups_Landlord)
    End Sub

    Public Overridable Sub UpdateCups_Landlord(ByVal currentcups_Landlord As cups_Landlord)
        Me.DataContext.cups_Landlords.Attach(currentcups_Landlord, Me.ChangeSet.GetOriginal(currentcups_Landlord))
    End Sub

    Public Overridable Sub DeleteCups_Landlord(ByVal cups_Landlord As cups_Landlord)
        Me.DataContext.cups_Landlords.Attach(cups_Landlord)
        Me.DataContext.cups_Landlords.DeleteOnSubmit(cups_Landlord)
    End Sub

    'TODO: Consider
    ' 1. Adding parameters to this method and constraining returned results, and/or
    ' 2. Adding query methods taking different parameters.
    Public Overridable Function GetCups_PropertyLandlords() As IQueryable(Of cups_PropertyLandlord)
        Return Me.DataContext.cups_PropertyLandlords
    End Function

    Public Overridable Sub InsertCups_PropertyLandlord(ByVal cups_PropertyLandlord As cups_PropertyLandlord)
        Me.DataContext.cups_PropertyLandlords.InsertOnSubmit(cups_PropertyLandlord)
    End Sub

    Public Overridable Sub UpdateCups_PropertyLandlord(ByVal currentcups_PropertyLandlord As cups_PropertyLandlord)
        Me.DataContext.cups_PropertyLandlords.Attach(currentcups_PropertyLandlord, Me.ChangeSet.GetOriginal(currentcups_PropertyLandlord))
    End Sub

    Public Overridable Sub DeleteCups_PropertyLandlord(ByVal cups_PropertyLandlord As cups_PropertyLandlord)
        Me.DataContext.cups_PropertyLandlords.Attach(cups_PropertyLandlord)
        Me.DataContext.cups_PropertyLandlords.DeleteOnSubmit(cups_PropertyLandlord)
    End Sub

    'TODO: Consider
    ' 1. Adding parameters to this method and constraining returned results, and/or
    ' 2. Adding query methods taking different parameters.
    Public Overridable Function GetCups_PropertyLandlordStatus() As IQueryable(Of cups_PropertyLandlordStatus)
        Return Me.DataContext.cups_PropertyLandlordStatus
    End Function

    Public Overridable Sub InsertCups_PropertyLandlordStatus(ByVal cups_PropertyLandlordStatus As cups_PropertyLandlordStatus)
        Me.DataContext.cups_PropertyLandlordStatus.InsertOnSubmit(cups_PropertyLandlordStatus)
    End Sub

    Public Overridable Sub UpdateCups_PropertyLandlordStatus(ByVal currentcups_PropertyLandlordStatus As cups_PropertyLandlordStatus)
        Me.DataContext.cups_PropertyLandlordStatus.Attach(currentcups_PropertyLandlordStatus, Me.ChangeSet.GetOriginal(currentcups_PropertyLandlordStatus))
    End Sub

    Public Overridable Sub DeleteCups_PropertyLandlordStatus(ByVal cups_PropertyLandlordStatus As cups_PropertyLandlordStatus)
        Me.DataContext.cups_PropertyLandlordStatus.Attach(cups_PropertyLandlordStatus)
        Me.DataContext.cups_PropertyLandlordStatus.DeleteOnSubmit(cups_PropertyLandlordStatus)
    End Sub

    'TODO: Consider
    ' 1. Adding parameters to this method and constraining returned results, and/or
    ' 2. Adding query methods taking different parameters.
    Public Overridable Function GetCups_StatusUpdateTypes() As IQueryable(Of cups_StatusUpdateType)
        Return Me.DataContext.cups_StatusUpdateTypes
    End Function

    Public Overridable Sub InsertCups_StatusUpdateType(ByVal cups_StatusUpdateType As cups_StatusUpdateType)
        Me.DataContext.cups_StatusUpdateTypes.InsertOnSubmit(cups_StatusUpdateType)
    End Sub

    Public Overridable Sub UpdateCups_StatusUpdateType(ByVal currentcups_StatusUpdateType As cups_StatusUpdateType)
        Me.DataContext.cups_StatusUpdateTypes.Attach(currentcups_StatusUpdateType, Me.ChangeSet.GetOriginal(currentcups_StatusUpdateType))
    End Sub

    Public Overridable Sub DeleteCups_StatusUpdateType(ByVal cups_StatusUpdateType As cups_StatusUpdateType)
        Me.DataContext.cups_StatusUpdateTypes.Attach(cups_StatusUpdateType)
        Me.DataContext.cups_StatusUpdateTypes.DeleteOnSubmit(cups_StatusUpdateType)
    End Sub

    Public Overridable Function DeleteClient(ByVal id As Integer) As Boolean Implements IClientDomainService.DeleteClient
        Dim c = GetClient(id)
        If (Not IsNothing(c)) Then
            _clientRepo.Remove(c)
            _clientRepo.SubmitChanges()
            Return True
        Else
            Return False
        End If
    End Function
    <RequiresAuthentication()> _
    Public Overridable Function GetClient(ByVal id As Integer) As Data.cups_Client Implements IClientDomainService.GetClient
        Dim c = From cs In GetAllClients() _
              Where cs.ID = id _
              Select cs
        If (c.Any()) Then
            Return c.First()
        Else
            Return Nothing
        End If
    End Function
    <RequiresAuthentication()> _
    Public Overridable Function GetClients() As System.Linq.IQueryable(Of Data.cups_Client)
        Dim clients As IQueryable(Of cups_Client) = From c In GetCups_Clients() _
                                                    Where c.Active = True _
                                                    Select c
        Return clients
    End Function

    <RequiresAuthentication()> _
   Public Overridable Function GetClientsNoExitStatus() As System.Linq.IQueryable(Of Data.cups_Client)
        Dim clients As IQueryable(Of cups_Client) = From c In GetCups_Clients() _
                                                    Where c.Active = True _
                                                    And Not c.cups_CurrentClientStatus.cups_StatusUpdateType.Name.Contains("Exit") _
                                                    Select c
        Return clients
    End Function

    <RequiresAuthentication()> _
    Public Overridable Function NewClient(ByVal client As Data.cups_Client) As Integer Implements IClientDomainService.NewClient
        InsertCups_Client(client)
        Return client.ID
    End Function
    <RequiresAuthentication()> _
    Public Overridable Function UpdateClient(ByVal client As Data.cups_Client) As Boolean Implements IClientDomainService.UpdateClient
        UpdateCups_Client(client)
    End Function
    <RequiresAuthentication()> _
    Public Overridable Function CompleteInterviewQestion(ByVal interviewTemplateQuestionID As Integer, ByVal interviewID As Integer, ByVal answer As String) As Integer Implements IInterviewDomainService.CompleteInterviewQestion
        Dim interviewQuestion As New cups_InterviewQuestion
        interviewQuestion.InterviewTemplateQuestionID = interviewTemplateQuestionID
        interviewQuestion.Answer = answer
        Return interviewQuestion.ID

    End Function

    <RequiresAuthentication()> _
    Public Overridable Function NewInterview(ByVal templateID As Integer, ByVal clientID As Integer) As Integer Implements IInterviewDomainService.NewInterview
        Dim interview As New cups_Interview
        interview.ClientID = clientID
        interview.InterviewTemplateID = templateID
        _interviewRepo.Add(interview)
        _interviewRepo.SubmitChanges()
        Return interview.ID
    End Function
    <RequiresAuthentication()> _
    Public Overridable Function NewInterviewTemplate(ByVal template As Data.cups_InterviewTemplate) As Integer Implements IInterviewDomainService.NewInterviewTemplate
        _interviewTemplateRepo.Add(template)
        _interviewTemplateRepo.SubmitChanges()
        Return template.ID
    End Function
    <RequiresAuthentication()> _
    Public Overridable Function NewInterviewTemplateQuestion(ByVal templateQuestion As Data.cups_InterviewTemplateQuestion) As Integer Implements IInterviewDomainService.NewInterviewTemplateQuestion
        _interviewTemplateQuestionRepo.Add(templateQuestion)
        _interviewTemplateQuestionRepo.SubmitChanges()
        Return templateQuestion.ID

    End Function
    <RequiresAuthentication()> _
    Public Overridable Function GetClientExitInterview(ByVal clientID As Integer) As cups_Interview Implements IInterviewDomainService.GetClientExitInterview
        Dim qry = From q In _interviewRepo.Query() _
                  Where q.ClientID = clientID And q.cups_InterviewTemplate.Name = "Exit" _
                  Select q
        Return qry.First()
    End Function
    <RequiresAuthentication()> _
    Public Function GetClientSummary() As System.Linq.IQueryable(Of Data.ClientSummary) Implements IClientDomainService.GetClientSummary
        Dim clients = From c In GetClients() _
                     Select New With {c.ID, c.LastName, c.FirstName, c.cups_CurrentClientStatus.cups_StatusUpdateType.Name, c.DateStarted, c.cups_CurrentClientStatus.UpdatedOn}


        Dim cSummaries As New List(Of ClientSummary)
        Dim cSummary As ClientSummary

        For Each c In clients
            cSummary = New ClientSummary()
            cSummary.ID = c.ID
            cSummary.FirstName = c.FirstName
            cSummary.LastName = c.LastName
            cSummary.CurrentStatus = c.Name
            cSummary.DateStarted = c.DateStarted
            If (IsNothing(c.UpdatedOn)) Then
                cSummary.LastUpdateDays = 0
            Else
                cSummary.LastUpdateDays = DateTime.Now.Subtract(c.UpdatedOn).Days
            End If
            cSummaries.Add(cSummary)
        Next
        Return cSummaries.AsQueryable()
    End Function
    <RequiresAuthentication()> _
    Public Function GetLandlord(ByVal LandlordID As Integer) As List(Of cups_Landlord)

        Dim landlords As IQueryable(Of cups_Landlord) = Nothing

        landlords = From existingLandlords In _repoLandlord.Query() _
                    Where existingLandlords.ID = LandlordID _
                    Select existingLandlords

        Return landlords.ToList()

    End Function
    <RequiresAuthentication()> _
    Public Function GetLandlordForEdit(ByVal LandlordID As Integer) As cups_Landlord

        Dim landlords = New cups_Landlord

        landlords = (From existingLandlords In _repoLandlord.Query() _
                    Where existingLandlords.ID = LandlordID _
                    Select existingLandlords).SingleOrDefault

        Return landlords

    End Function

    <RequiresAuthentication()> _
    Public Function GetPropertyForEdit(ByVal PropertyID As Integer) As cups_PropertyLandlord

        Dim LandlordProperty = New cups_PropertyLandlord

        LandlordProperty = (From existingProperty In _repoProperty.Query() _
                     Where existingProperty.ID = PropertyID _
                     Select existingProperty).SingleOrDefault

        Return LandlordProperty

    End Function

    <RequiresAuthentication()> _
    Public Function ClientPropertyExist(ByVal PropertyID As Integer) As Boolean

        Dim cpl = New cups_ClientPropertyLandlord

        cpl = (From existingProperty In _clientPropertyLandlordRepo.Query() _
                     Where existingProperty.ID = PropertyID _
                     Select existingProperty).SingleOrDefault

        If Not IsNothing(cpl) Then
            Return True
        End If

        Return False

    End Function


    <RequiresAuthentication()> _
    Public Function GetLandlords() As List(Of cups_Landlord)

        Dim landlords As IQueryable(Of cups_Landlord) = Nothing

        landlords = From existingLandlords In _repoLandlord.Query() _
                    Select existingLandlords

        Return landlords.ToList()

    End Function

    <RequiresAuthentication()> _
    Public Function GetProperties(ByVal LandlordID As Integer) As List(Of cups_PropertyLandlord)

        Dim properties As IQueryable(Of cups_PropertyLandlord) = Nothing

        properties = From existingProperty In _repoProperty.Query() _
                     Where existingProperty.LandlordID = LandlordID _
                     Select existingProperty

        Return properties.ToList()

    End Function

    Public Function GetPropertiesWithStatus(ByVal LandlordID As Integer) As List(Of PropertyStatus)

        Dim PropStatus As PropertyStatus
        Dim psList As New List(Of PropertyStatus)
        Dim properties = From p In _repoProperty.Query() _
                     Where p.LandlordID = LandlordID _
                     Select New With {p.ID, p.Address, p.City, p.PostalCode, p.Suite, .PropertyStatus = p.cups_PropertyLandlordStatus.Name, p.UpdatedOn, p.UpdatedBy}

        For Each p In properties
            PropStatus = New PropertyStatus
            PropStatus.ID = p.ID
            PropStatus.Address = p.Address
            PropStatus.City = p.City
            PropStatus.PostalCode = p.PostalCode
            PropStatus.Suite = p.Suite
            PropStatus.Status = p.PropertyStatus
            PropStatus.UpdatedOn = p.UpdatedOn
            PropStatus.UpdatedBy = p.UpdatedBy
            psList.Add(PropStatus)
        Next

        Return psList

    End Function

    <RequiresAuthentication()> _
    Public Function GetClientsForProperty(ByVal PropertyID As Integer) As List(Of ClientProperty)

        Dim ClientProp As ClientProperty
        Dim cpList As New List(Of ClientProperty)
        Dim clients = From c In _clientPropertyLandlordRepo.Query() _
                     Where c.PropertyLandlordID = PropertyID _
                     Select New With {c.ID, c.ClientID, c.DateStart, c.DateEnd, .FirstName = c.cups_Client.FirstName, .LastName = c.cups_Client.LastName}

        For Each p In clients
            ClientProp = New ClientProperty
            ClientProp.ID = p.ClientID
            ClientProp.Name = p.FirstName & " " & p.LastName
            If Not IsNothing(p.DateStart) Then
                ClientProp.FromDate = p.DateStart
            End If
            If Not IsNothing(p.DateEnd) Then
                ClientProp.ToDate = p.DateEnd
            End If

            cpList.Add(ClientProp)
        Next

        Return cpList
    End Function

    <RequiresAuthentication()> _
    Public Sub SaveLandlordProperties(ByVal properties As cups_PropertyLandlord)

        _repoProperty.Add(properties)
        _repoProperty.SubmitChanges()

    End Sub
    <RequiresAuthentication()> _
    Public Sub SaveLandlord(ByVal landlord As cups_Landlord)

        _repoLandlord.Add(landlord)
        _repoLandlord.SubmitChanges()

    End Sub
    <RequiresAuthentication()> _
    Public Sub UpdateProperty(ByVal objProperty As cups_PropertyLandlord, ByVal PropertyID As Integer)
        Try


            Dim qry = From existingProperty In _repoProperty.Query() _
                        Where existingProperty.ID = PropertyID _
                        Select existingProperty

            If (qry.Count() > 0) Then
                Dim PropertyToUpdate = qry.First()

                With objProperty
                    PropertyToUpdate.Address = .Address
                    PropertyToUpdate.City = .City
                    PropertyToUpdate.PostalCode = .PostalCode
                    PropertyToUpdate.Suite = .Suite
                    PropertyToUpdate.CurrentStatusID = .CurrentStatusID
                    PropertyToUpdate.UpdatedBy = .UpdatedBy
                    PropertyToUpdate.UpdatedOn = .UpdatedOn
                End With

            End If

            _repoProperty.SubmitChanges()

        Catch ex As Exception
            Throw ex
        End Try

    End Sub
    <RequiresAuthentication()> _
    Public Sub UpdateLandlord(ByVal landlord As cups_Landlord, ByVal LandlordID As Integer)
        Try


            Dim qry = From existingLandlords In _repoLandlord.Query() _
                        Where existingLandlords.ID = LandlordID _
                        Select existingLandlords

            If (qry.Count() > 0) Then
                Dim LandlordToUpdate = qry.First()

                With landlord
                    LandlordToUpdate.Name = .Name
                    LandlordToUpdate.Phone = .Phone
                    LandlordToUpdate.CompanyName = .CompanyName
                    LandlordToUpdate.Notes = .Notes
                    LandlordToUpdate.UpdatedBy = .UpdatedBy
                    LandlordToUpdate.UpdatedOn = .UpdatedOn
                End With

            End If

            _repoLandlord.SubmitChanges()

        Catch ex As Exception
            Throw ex
        End Try

    End Sub
    <RequiresAuthentication()> _
    Public Function PropertyStatusLandlord() As IList(Of cups_PropertyLandlordStatus)
        Dim status = From p In _PropertyLandlordStatus.Query() _
                 Select p
        Return status.ToList()

    End Function
    <RequiresAuthentication()> _
    Public Function GetClientSummary(ByVal maximumRows As Integer, ByVal startRowIndex As Integer, ByVal sortType As String, ByVal myClientsOnly As Boolean) As IList(Of ClientSummary)

        If (Not myClientsOnly) Then
            Return GetClientSummary(maximumRows, startRowIndex, sortType)
        End If

        Dim clients As IQueryable

        Dim sortType1 As String = String.Empty
        If (sortType.Contains("LastUpdateDays")) Then
            sortType = sortType.Replace("LastUpdateDays", "DateStarted")
        End If
        If (sortType.Contains("CurrentStatus")) Then
            sortType1 = sortType
            sortType = ""
        End If
        If (maximumRows > 0) Then
            clients = (From c In GetClients() _
                        .SortBy(sortType) _
                        Where c.UpdateBy = GetCurrentUser() _
                        Select New With {c.ID, c.LastName, c.FirstName, .CurrentStatus = c.cups_CurrentClientStatus.cups_StatusUpdateType.Name, c.DateStarted, c.cups_CurrentClientStatus.UpdatedOn}).SortBy(sortType1) _
                        .Skip(startRowIndex) _
                        .Take(maximumRows)

        Else
            clients = (From c In GetClients() _
                        .SortBy(sortType) _
                        Where c.UpdateBy = GetCurrentUser() _
                        Select New With {c.ID, c.LastName, c.FirstName, .CurrentStatus = c.cups_CurrentClientStatus.cups_StatusUpdateType.Name, c.DateStarted, c.cups_CurrentClientStatus.UpdatedOn}).SortBy(sortType1) _
                        .Skip(startRowIndex)
        End If

        Dim cSummaries As New List(Of ClientSummary)

        Dim cSummary As ClientSummary

        For Each c In clients

            cSummary = New ClientSummary()
            cSummary.ID = c.ID
            cSummary.FirstName = c.FirstName
            cSummary.LastName = c.LastName
            cSummary.CurrentStatus = c.CurrentStatus
            cSummary.DateStarted = c.DateStarted
            If (IsNothing(c.UpdatedOn)) Then
                cSummary.LastUpdateDays = 0
            Else
                cSummary.LastUpdateDays = DateTime.Now.Subtract(c.UpdatedOn).Days
            End If

            If IsNothing(c.CurrentStatus) Then : cSummaries.Add(cSummary)
            ElseIf Not c.CurrentStatus.ToString.Contains("Exit") Then : cSummaries.Add(cSummary)
            End If

        Next

        Return cSummaries.ToList()
    End Function


    <RequiresAuthentication()> _
    Public Function GetClientSummary(ByVal maximumRows As Integer, ByVal startRowIndex As Integer, ByVal sortType As String) As IList(Of ClientSummary)
        Dim clients As IQueryable

        Dim sortType1 As String = String.Empty
        If (sortType.Contains("LastUpdateDays")) Then
            sortType = sortType.Replace("LastUpdateDays", "DateStarted")
        End If
        If (sortType.Contains("CurrentStatus")) Then
            sortType1 = sortType
            sortType = ""
        End If
        If (maximumRows > 0) Then
            clients = (From c In GetClients() _
                        .SortBy(sortType) _
                        Select New With {c.ID, c.LastName, c.FirstName, .CurrentStatus = c.cups_CurrentClientStatus.cups_StatusUpdateType.Name, c.DateStarted, c.cups_CurrentClientStatus.UpdatedOn}).SortBy(sortType1) _
                         .Skip(startRowIndex) _
                        .Take(maximumRows)
        Else
            clients = (From c In GetClients() _
                        .SortBy(sortType) _
                        Select New With {c.ID, c.LastName, c.FirstName, .CurrentStatus = c.cups_CurrentClientStatus.cups_StatusUpdateType.Name, c.DateStarted, c.cups_CurrentClientStatus.UpdatedOn}).SortBy(sortType1) _
                        .Skip(startRowIndex)
        End If


        Dim cSummaries As New List(Of ClientSummary)

        Dim cSummary As ClientSummary

        For Each c In clients

            cSummary = New ClientSummary()
            cSummary.ID = c.ID
            cSummary.FirstName = c.FirstName
            cSummary.LastName = c.LastName
            cSummary.CurrentStatus = c.CurrentStatus
            cSummary.DateStarted = c.DateStarted
            If (IsNothing(c.UpdatedOn)) Then
                cSummary.LastUpdateDays = 0
            Else
                cSummary.LastUpdateDays = DateTime.Now.Subtract(c.UpdatedOn).Days
            End If

            If IsNothing(c.CurrentStatus) Then : cSummaries.Add(cSummary)
            ElseIf Not c.CurrentStatus.ToString.Contains("Exit") Then : cSummaries.Add(cSummary)
            End If

        Next

        Return cSummaries.ToList()
    End Function


    <RequiresAuthentication()> _
    Public Function SearchClients(ByVal maximumRows As Integer, ByVal startRowIndex As Integer, ByVal data As PersonalInterview) As System.Collections.Generic.IList(Of Data.cups_Client) Implements IClientDomainService.SearchClients

        If (IsNothing(data)) Then
            Return New List(Of cups_Client)
        End If
        Dim qry = From c In GetClients() _
                  Where c.FirstName.Contains(String.Format("{0}", data.FirstName)) And _
                  c.LastName.Contains(String.Format("{0}", data.LastName)) And _
                  (String.IsNullOrEmpty(data.MiddleName) Or c.MiddleName.Contains(String.Format("{0}", data.MiddleName))) And _
                  (String.IsNullOrEmpty(data.Suffix) Or c.Suffix.Contains(String.Format("{0}", data.Suffix))) And _
                  (String.IsNullOrEmpty(data.Salutation) Or c.Saluation.Contains(String.Format("{0}", data.Salutation))) And _
                  (String.IsNullOrEmpty(data.OtherAlias) Or c.Alias.Contains(String.Format("{0}", data.OtherAlias))) And _
                   (String.IsNullOrEmpty(data.Email) Or c.Email.Contains(String.Format("{0}", data.Email))) And _
                   (String.IsNullOrEmpty(data.Phone) Or c.Phone.Contains(String.Format("{0}", data.Phone))) And _
                   (String.IsNullOrEmpty(data.Gender) Or c.Gender.Contains(String.Format("{0}", data.Gender))) And _
                   (String.IsNullOrEmpty(data.sin) Or c.Sin.Contains(String.Format("{0}", data.sin))) _
                  Select c

        '  (IsNothing(c.BirthDate) Or c.BirthDate = data.BirthDate) And _
        '         (IsNothing(c.InterviewDate) Or c.InterviewDate = data.InterviewDate) And _
        '         (IsNothing(c.EntryDate) Or c.EntryDate = data.EntryDate) And _
        '         (IsNothing(c.FirstPlacementDate) Or c.FirstPlacementDate = data.FirstPlacementDate) _

        'Get the Template ID
        Dim tIDQry = From q In _interviewTemplateRepo.Query() _
                     Where q.Name = Constants.Personal.PERSONALINTERVIEW _
                     Select q.ID
        Dim qry1 As IQueryable(Of cups_Client)
        If (maximumRows > 0) Then
            qry1 = From q In SearchInterviewData(data, tIDQry.First(), qry) _
                                                 .Skip(startRowIndex) _
                                                .Take(maximumRows) _
                                                Select q
        Else
            qry1 = From q In SearchInterviewData(data, tIDQry.First(), qry) _
                    .Skip(startRowIndex) _
                    Select q
        End If




        Return qry1.ToList()

    End Function


    <RequiresAuthentication()> _
    Public Function GetPersonalInterview(ByVal clientID As Integer) As PersonalInterview Implements IInterviewDomainService.GetPersonalInterview
        Dim pView As PersonalInterview
        Dim c As cups_Client


        Dim client = From cq In _clientRepo.Query() _
                     Where cq.ID = clientID _
                     Select cq

        If (client.Count() > 0) Then
            c = client.First()
        Else
            Return New PersonalInterview
        End If


        pView = New PersonalInterview(GetInterviewData(clientID, Constants.Personal.PERSONALINTERVIEW))
        pView.FirstName = c.FirstName
        pView.LastName = c.LastName
        pView.MiddleName = c.MiddleName
        pView.Suffix = c.Suffix
        pView.Salutation = c.Saluation
        pView.BirthDate = String.Format("{0:d}", c.BirthDate)
        pView.OtherAlias = c.Alias
        pView.Phone = c.Phone
        pView.sin = c.Sin
        pView.Gender = c.Gender
        pView.ClientID = c.ID
        pView.Email = c.Email

        pView.InterviewDate = String.Format("{0:d}", c.InterviewDate)
        pView.EntryDate = String.Format("{0:d}", c.EntryDate)
        pView.FirstPlacementDate = String.Format("{0:d}", c.FirstPlacementDate)

        Return pView

    End Function
    <RequiresAuthentication()> _
    Public Function UpdatePersonalInterview(ByVal clientID As String, ByVal pView As PersonalInterview) As Integer Implements IInterviewDomainService.UpdatePersonalInterview

        Dim qry = From c In _clientRepo.Query() _
                  Where c.ID = clientID _
                  Select c
        If (qry.Count() > 0) Then
            Dim client = qry.First()
            client.FirstName = pView.FirstName
            client.LastName = pView.LastName
            client.MiddleName = pView.MiddleName
            client.Suffix = pView.Suffix
            client.Saluation = pView.Salutation
            If (String.IsNullOrEmpty(pView.BirthDate)) Then
                client.BirthDate = Nothing
            Else
                client.BirthDate = DateTime.Parse(pView.BirthDate)
            End If

            client.Alias = pView.OtherAlias
            client.Phone = pView.Phone
            client.Sin = pView.sin
            client.Gender = pView.Gender
            client.UpdateBy = GetCurrentUser()
            client.UpdatedOn = DateTime.Now
            client.Email = pView.Email
            Dim iDate As DateTime
            If (Not DateTime.TryParse(pView.InterviewDate, iDate)) Then
                client.InterviewDate = Nothing
            Else
                client.InterviewDate = iDate
            End If

            Dim eDate As DateTime
            If (Not DateTime.TryParse(pView.EntryDate, eDate)) Then
                client.EntryDate = Nothing
            Else
                client.EntryDate = eDate
            End If

            Dim fDate As DateTime
            If (Not DateTime.TryParse(pView.FirstPlacementDate, fDate)) Then
                client.FirstPlacementDate = Nothing
            Else
                client.FirstPlacementDate = fDate
            End If




            _clientRepo.SubmitChanges()

            UpdateInterviewData(client.ID, Constants.Personal.PERSONALINTERVIEW, pView)

        Else
            Throw New NotImplementedException
        End If

    End Function
    <RequiresAuthentication()> _
    Public Function InsertPersonalInterview(ByVal pView As PersonalInterview) As Integer Implements IInterviewDomainService.InsertPersonalInterview
        Dim client As New cups_Client
        client.FirstName = pView.FirstName
        client.LastName = pView.LastName
        client.MiddleName = pView.MiddleName
        client.Suffix = pView.Suffix
        client.Saluation = pView.Salutation
        Dim bDate As DateTime
        If (DateTime.TryParse(pView.BirthDate, bDate)) Then
            client.BirthDate = bDate
        Else
            client.BirthDate = Nothing
        End If

        client.Alias = pView.OtherAlias
        client.Phone = pView.Phone
        client.Sin = pView.sin
        client.Gender = pView.Gender
        client.DateStarted = DateTime.Now
        client.UpdateBy = GetCurrentUser()
        client.UpdatedOn = DateTime.Now
        client.Active = True
        Dim iDate As DateTime
        If (DateTime.TryParse(pView.InterviewDate, iDate)) Then
            client.InterviewDate = iDate
        Else
            client.InterviewDate = Nothing
        End If
        Dim eDate As DateTime
        If (DateTime.TryParse(pView.EntryDate, eDate)) Then
            client.EntryDate = eDate
        Else
            client.EntryDate = Nothing
        End If

        Dim fDate As DateTime
        If (DateTime.TryParse(pView.FirstPlacementDate, fDate)) Then
            client.FirstPlacementDate = fDate
        Else
            client.FirstPlacementDate = Nothing
        End If

        _clientRepo.Add(client)
        _clientRepo.SubmitChanges()

        CreateInterview(client.ID, Constants.Personal.PERSONALINTERVIEW)
        UpdateInterviewData(client.ID, Constants.Personal.PERSONALINTERVIEW, pView)
        Return client.ID

    End Function
    <RequiresAuthentication()> _
    Public Function GetExitInterview(ByVal clientID As Integer) As ExitInterview
        Dim data As ExitInterview = New ExitInterview(GetInterviewData(clientID, Constants.ExitInterview.INTERVIEW))
        Dim qry = From q In _clientRepo.Query() _
                 Where q.ID = clientID _
                 Select q


        Dim client = qry.First()
        data.FirstName = client.FirstName
        data.LastName = client.LastName
        data.ClientID = client.ID

        If (IsNothing(data)) Then
            'make a new interview
            CreateExitInterview(clientID)
            Return GetExitInterview(clientID)
        Else
            Return data
        End If
    End Function

    Public Function UpdateExitInterview(ByVal clientID As Integer, ByVal data As ExitInterview) As Integer

        UpdateInterviewData(clientID, Constants.ExitInterview.INTERVIEW, data)
    End Function
    <RequiresAuthentication()> _
    Public Function CreateExitInterview(ByVal clientID As Integer) As Integer
        CreateInterview(clientID, Constants.ExitInterview.INTERVIEW)
    End Function
    <RequiresAuthentication()> _
    Public Function GetBackgroundInterview(ByVal clientID As Integer) As InterviewData
        Dim data = GetInterviewData(clientID, Constants.BackgroundInterview.INTERVIEW)
        If (IsNothing(data)) Then
            CreateInterview(clientID, Constants.BackgroundInterview.INTERVIEW)
            data = GetInterviewData(clientID, Constants.BackgroundInterview.INTERVIEW)
        End If
        Return data
    End Function

    <RequiresAuthentication()> _
    Public Function UpdateBackgroundInterview(ByVal clientID As Integer, ByVal data As InterviewData) As Integer
        UpdateInterviewData(clientID, Constants.BackgroundInterview.INTERVIEW, data)
    End Function

    'Returns new interview ID
    <RequiresAuthentication()> _
    Public Function CreateInterview(ByVal clientID As Integer, ByVal templateName As String) As Integer
        Dim qry = From q In _interviewTemplateQuestionRepo.Query() _
                 Where q.cups_InterviewTemplate.Name = templateName _
                 Select q
        'TODO also by application ID
        If (qry.Count > 0) Then
            Dim interviewTemplate As cups_InterviewTemplate = qry.First().cups_InterviewTemplate
            Dim interview As New cups_Interview
            interview.ClientID = clientID
            interview.Description = templateName
            interview.InterviewTemplateID = interviewTemplate.ID
            interview = _interviewRepo.Add(interview)
            _interviewRepo.SubmitChanges()

            For Each itm In qry
                Dim iQuestion As New cups_InterviewQuestion
                iQuestion.Question = itm.Question
                iQuestion.Answer = ""
                iQuestion.InterviewTemplateQuestionID = itm.ID
                iQuestion.InterviewID = interview.ID

                _interviewQuestionRepo.Add(iQuestion)

            Next
            interview.UpdatedBy = GetCurrentUser()
            interview.UpdatedOn = DateTime.Now
        End If
        _interviewRepo.SubmitChanges()
        _interviewQuestionRepo.SubmitChanges()

    End Function
    <RequiresAuthentication()> _
    Protected Function GetInterviewData(ByVal clientID As Integer, ByVal templateName As String) As InterviewData
        Dim nIndex As Integer = 0
        Dim pView As New InterviewData


        'look for new template question, if there is a new one add the template question to question list
        Dim interviewQry As IQueryable(Of cups_Interview) = From q In _interviewRepo.Query() _
                            Where q.ClientID = clientID And q.cups_InterviewTemplate.Name = templateName _
                            Select q
        If (interviewQry.Count() > 0) Then
            'Add this to it later:: And q.cups_Client.ApplicationID = q.cups_InterviewTemplate.ApplicationID 



            Dim tempQry = From tq In interviewQry.First().cups_InterviewTemplate.cups_InterviewTemplateQuestions _
                          Select tq

            Dim ctlQry = From q1 In _interviewQuestionRepo.Query() _
                        Where q1.cups_Interview.ClientID = clientID And q1.cups_Interview.cups_InterviewTemplate.Name = templateName _
                        Select q1

            Dim missingQry = From itemp In tempQry _
                             Group Join iQues In ctlQry On itemp.ID Equals iQues.InterviewTemplateQuestionID Into myMissing = Group _
                             From iQues In myMissing.DefaultIfEmpty _
                             Where IsNothing(iQues) _
                             Select itemp

            For Each i In missingQry
                Dim iq As cups_InterviewQuestion = New cups_InterviewQuestion
                iq.InterviewID = interviewQry.First().ID
                iq.InterviewTemplateQuestionID = i.ID
                iq.Question = i.Question
                _interviewQuestionRepo.Add(iq)
                _interviewQuestionRepo.SubmitChanges()
            Next

            For Each item In ctlQry
                nIndex = 0
                pView.TemplateID = item.cups_Interview.InterviewTemplateID
                Dim arrayAnswer(0) As String
                Dim arrayControls(0) As String
                If (Not String.IsNullOrEmpty(item.Answer)) Then
                    arrayAnswer = item.Answer.Split(",")
                End If
                If (Not String.IsNullOrEmpty(item.ControlIDs)) Then
                    arrayControls = item.ControlIDs.Split(",")
                End If

                For Each ctlItem In item.cups_InterviewTemplateQuestion.cups_ControlMappings
                    If (Not ctlItem.IsAnswer) Then
                        If (Not pView.QuestionsText.ContainsKey(ctlItem.ControlID)) Then
                            pView.QuestionsText.Add(ctlItem.ControlID, item.Question)
                        Else
                            pView.QuestionsText(ctlItem.ControlID) = item.Question
                        End If
                    Else
                        If (arrayControls.Count() <> arrayAnswer.Count()) Then
                            'single answer
                            If (Not pView.AnswersText.ContainsKey(ctlItem.ControlID)) Then
                                pView.AnswersText.Add(ctlItem.ControlID, item.Answer)
                            Else
                                pView.AnswersText(ctlItem.ControlID) = item.Answer
                            End If

                        Else
                            If (Not IsNothing(arrayControls)) Then
                                If (arrayControls.Count() > nIndex) Then
                                    Dim ctlName As String = arrayControls(nIndex)
                                    If (ctlName = ctlItem.ControlID) Then
                                        If (Not pView.AnswersText.ContainsKey(ctlItem.ControlID)) Then
                                            pView.AnswersText.Add(ctlItem.ControlID, arrayAnswer(nIndex))
                                        Else
                                            pView.AnswersText(ctlItem.ControlID) = arrayAnswer(nIndex)
                                        End If

                                        nIndex = nIndex + 1
                                    End If
                                End If

                            End If
                        End If

                    End If



                Next
            Next
            Return pView
        Else
            Return Nothing
        End If

    End Function
    <RequiresAuthentication()> _
    Protected Function UpdateInterviewData(ByVal clientID As Integer, ByVal interviewName As String, ByVal data As InterviewData) As Integer
        Dim nIndex As Integer = 0
        Dim qry = From q In _interviewQuestionRepo.Query() _
                 Where q.cups_Interview.cups_InterviewTemplate.Name = interviewName And q.cups_Interview.ClientID = clientID _
                 Select q
        If (qry.Count() = 0) Then
            CreateInterview(clientID, interviewName)
            UpdateInterviewData(clientID, interviewName, data)
        End If
        If (qry.Count() > 0) Then
            'Get a list of interview questions and corresponding mapping
            For Each itm In qry
                nIndex = 0
                itm.Answer = ""
                itm.ControlIDs = ""
                For Each map In itm.cups_InterviewTemplateQuestion.cups_ControlMappings
                    If (Not String.IsNullOrEmpty(data.AnswersText(map.ControlID))) Then

                        If (map.IsAnswer) Then
                            If (nIndex = 0) Then
                                itm.Answer = ""
                                itm.ControlIDs = ""
                            Else
                                itm.Answer = itm.Answer & ","
                                itm.ControlIDs = itm.ControlIDs & ","
                            End If
                            itm.Answer = itm.Answer & data.AnswersText(map.ControlID)
                            itm.ControlIDs = itm.ControlIDs & map.ControlID
                        Else
                            itm.Question = itm.Question
                        End If
                        nIndex = nIndex + 1
                    End If

                Next

            Next
        End If
        _interviewQuestionRepo.SubmitChanges()
    End Function

    <RequiresAuthentication()> _
    Public Function GetHealthLegalInterview(ByVal clientID As Integer) As InterviewData
        Dim data = GetInterviewData(clientID, Constants.HealthLegalInterview.INTERVIEW)
        If (IsNothing(data)) Then
            CreateInterview(clientID, Constants.HealthLegalInterview.INTERVIEW)
            data = GetInterviewData(clientID, Constants.HealthLegalInterview.INTERVIEW)
        End If
        Return data
    End Function
    <RequiresAuthentication()> _
    Public Function UpdateHealthLegalInterview(ByVal clientID As Integer, ByVal data As InterviewData) As Integer
        UpdateInterviewData(clientID, Constants.HealthLegalInterview.INTERVIEW, data)

    End Function
    <RequiresAuthentication()> _
    Public Function GetFollowupInterview(ByVal clientID As Integer, ByVal interviewType As String) As FollowupInterview
        Dim c As cups_Client
        Dim iData As InterviewData = GetInterviewData(clientID, interviewType)
        Dim data As FollowupInterview

        If (IsNothing(iData)) Then
            CreateInterview(clientID, interviewType)
            data = New FollowupInterview(GetInterviewData(clientID, interviewType))
        Else
            data = New FollowupInterview(iData)
        End If
        Dim client = From cq In _clientRepo.Query() _
                    Where cq.ID = clientID _
                    Select cq

        If (client.Count() > 0) Then
            c = client.First()
        End If

        data.ClientID = c.ID
        data.Salutation = c.Saluation
        data.Suffix = c.Suffix
        data.FirstName = c.FirstName
        data.MiddleName = c.MiddleName
        data.LastName = c.LastName
        Return data

    End Function
    <RequiresAuthentication()> _
    Public Function UpdateFollowupInterview(ByVal clientID As Integer, ByVal interviewType As String, ByVal data As FollowupInterview) As Integer
        UpdateInterviewData(clientID, interviewType, data)
    End Function
    <RequiresAuthentication()> _
    Public Function GetClientStatusList(ByVal clientID As Integer) As List(Of ClientStatus)
        Dim cStatii As New List(Of ClientStatus)

        Dim cs = From ct In _statusRepo.Query() _
                 Where ct.ClientID = clientID _
                 Select ct

        For Each c In cs
            Dim cStatus As New ClientStatus
            cStatus.ID = c.ID
            cStatus.Description = c.Description
            If (Not IsNothing(c.cups_PropertyLandlord) AndAlso (Not IsNothing(c.cups_PropertyLandlord.cups_Landlord))) Then
                cStatus.Landlord = c.cups_PropertyLandlord.cups_Landlord.Name
            End If

            If (Not IsNothing(c.cups_PropertyLandlord)) Then
                cStatus.PropertyLandlordID = c.cups_PropertyLandlord.ID
            Else
                cStatus.PropertyLandlordID = 0
            End If

            cStatus.LastUpdatedDate = c.UpdatedOn
            If (Not IsNothing(c.cups_StatusUpdateType)) Then
                cStatus.UpdateType = c.cups_StatusUpdateType.Name
                cStatus.UpdateTypeID = c.cups_StatusUpdateType.ID
            End If
            cStatus.StaffMember = String.Format("{0}", c.StaffMember)
            cStatii.Add(cStatus)
        Next
        Return cStatii
    End Function

    Public Function GetClientStatus(ByVal id As Integer) As ClientStatus
        Dim csStatus As New ClientStatus
        Try
            Dim cs = (From ct In _statusRepo.Query() _
                 Where ct.ID = id _
                 Select ct).FirstOrDefault()
            If (Not IsNothing(cs)) Then
                csStatus.ID = cs.ID
                csStatus.Description = cs.Description
                If (Not IsNothing(cs.cups_PropertyLandlord)) AndAlso (Not IsNothing(cs.cups_PropertyLandlord.cups_Landlord)) Then
                    csStatus.Landlord = cs.cups_PropertyLandlord.cups_Landlord.Name
                    csStatus.PropertyLandlordID = cs.cups_PropertyLandlord.ID
                Else
                    csStatus.PropertyLandlordID = 0
                End If
                csStatus.LastUpdatedDate = cs.UpdatedOn
                If (Not IsNothing(cs.cups_StatusUpdateType)) Then
                    csStatus.UpdateType = cs.cups_StatusUpdateType.Name
                    csStatus.UpdateTypeID = cs.cups_StatusUpdateType.ID
                End If
                csStatus.StaffMember = String.Format("{0}", cs.StaffMember)
            End If
        Catch ex As Exception
            ''TODO: Log this shizzle
        End Try


        Return csStatus
    End Function

    Public Function UpdateClientStatus(ByVal clientID As Integer, ByVal cs As ClientStatus) As Integer
        Throw New NotImplementedException()
    End Function
    <RequiresAuthentication()> _
    Public Function InsertClientStatus(ByVal clientID As Integer, ByVal cs As ClientStatus) As Integer
        Dim isExistingStatus As Boolean = False
        Dim cStatus As cups_ClientStatus

        cStatus = (From ct In _statusRepo.Query() _
                Where ct.ID = cs.ID _
                Select ct).FirstOrDefault()
        If (Not IsNothing(cStatus)) Then
            isExistingStatus = True
        Else
            cStatus = New cups_ClientStatus
        End If
        cStatus.ClientID = clientID
        cStatus.Description = cs.Description
        cStatus.InsertedOn = DateTime.Now
        cStatus.UpdatedBy = GetCurrentUser()
        cStatus.UpdatedOn = DateTime.Now
        cStatus.StatusUpdateTypeID = cs.UpdateTypeID
        cStatus.StaffMember = cs.StaffMember
        If (cs.PropertyLandlordID = 0) Then
            cStatus.LandlordPropertyID = Nothing
        Else
            cStatus.LandlordPropertyID = cs.PropertyLandlordID
        End If

        If (Not isExistingStatus) Then
            _statusRepo.Add(cStatus)
        End If

        _statusRepo.SubmitChanges()
        cStatus.cups_Client.CurrentStatusID = cStatus.ID
        _statusRepo.SubmitChanges()

    End Function
    <RequiresAuthentication()> _
    Public Function GetClientStatusTypes() As List(Of cups_StatusUpdateType)
        Dim qry = From s In _clientStatusUpdateTypeRepo.Query() _
                  Order By s.Name _
                  Select s
        Return qry.ToList()
    End Function
    <RequiresAuthentication()> _
    Public Function GetClientProperties(ByVal clientID As Integer) As List(Of ClientProperty)
        Dim clientProp As ClientProperty
        Dim cpList As New List(Of ClientProperty)
        Dim qry = From s In _clientPropertyLandlordRepo.Query() _
                 Where s.ClientID = clientID _
                 Select New With {s.ID, s.PropertyLandlordID, s.cups_PropertyLandlord.cups_Landlord.Name, s.DateStart, s.DateEnd, s.cups_PropertyLandlord.Address, s.cups_PropertyLandlord.Suite}

        For Each cp In qry
            clientProp = New ClientProperty
            clientProp.ID = cp.ID
            clientProp.PropertyID = cp.PropertyLandlordID
            clientProp.Name = String.Format("{0}", cp.Name)
            clientProp.FromDate = String.Format("{0:d}", cp.DateStart)
            clientProp.ToDate = String.Format("{0:d}", cp.DateEnd)
            clientProp.Address = String.Format("{0}", cp.Address)
            clientProp.Suite = String.Format("{0}", cp.Suite)
            cpList.Add(clientProp)
        Next
        
        Return cpList
    End Function
    <RequiresAuthentication()> _
    Public Function GetClientProperty(ByVal clientID As Integer) As ClientProperty
        Dim clientProp As New ClientProperty
        Try
            Dim cp = (From s In _clientPropertyLandlordRepo.Query() _
                  Where s.ID = clientID _
                  Select New With {s.ID, s.PropertyLandlordID, s.cups_PropertyLandlord.cups_Landlord.Name, s.DateStart, s.DateEnd, s.cups_PropertyLandlord.Address, s.cups_PropertyLandlord.Suite}).FirstOrDefault()

            If (Not IsNothing(cp)) Then
                clientProp = New ClientProperty
                clientProp.ID = cp.ID
                clientProp.PropertyID = cp.PropertyLandlordID
                clientProp.Name = String.Format("{0}", cp.Name)
                clientProp.FromDate = String.Format("{0:d}", cp.DateStart)
                clientProp.ToDate = String.Format("{0:d}", cp.DateEnd)
                clientProp.Address = String.Format("{0}", cp.Address)
                clientProp.Suite = String.Format("{0}", cp.Suite)
            End If

        Catch ex As Exception
            ''TODO: log this shizzle
        End Try

        Return clientProp
    End Function
    <RequiresAuthentication()> _
    Public Function GetClientProperties(ByVal clientID As Integer, ByVal maximumRows As Integer, ByVal startRowIndex As Integer) As IList(Of ClientProperty)
        Dim clientProp As ClientProperty
        Dim cpList As New List(Of ClientProperty)
        Dim qry

        If (maximumRows > 0) Then
            qry = From s In _clientPropertyLandlordRepo.Query() _
                Where s.ClientID = clientID _
                Skip (startRowIndex) _
                Take (maximumRows) _
                Select New With {s.ID, s.PropertyLandlordID, s.cups_PropertyLandlord.cups_Landlord.Name, s.DateStart, s.DateEnd, s.cups_PropertyLandlord.Address, s.cups_PropertyLandlord.Suite}
        Else
            qry = From s In _clientPropertyLandlordRepo.Query() _
                Where s.ClientID = clientID _
                Skip (startRowIndex) _
                Select New With {s.ID, s.PropertyLandlordID, s.cups_PropertyLandlord.cups_Landlord.Name, s.DateStart, s.DateEnd, s.cups_PropertyLandlord.Address, s.cups_PropertyLandlord.Suite}

        End If

        For Each cp In qry
            clientProp = New ClientProperty
            clientProp.ID = cp.ID
            clientProp.PropertyID = cp.PropertyLandlordID
            clientProp.Name = String.Format("{0}", cp.Name)
            clientProp.FromDate = String.Format("{0:d}", cp.DateStart)
            clientProp.ToDate = String.Format("{0:d}", cp.DateEnd)
            clientProp.Address = String.Format("{0}", cp.Address)
            clientProp.Suite = String.Format("{0}", cp.Suite)
            cpList.Add(clientProp)
        Next

        Return cpList
    End Function
    <RequiresAuthentication()> _
    Public Function UpdateClientProperty(ByVal clientID As Integer, ByVal data As ClientProperty) As Integer
        Dim fromDate As DateTime = DateTime.MinValue
        Dim toDate As DateTime = DateTime.MinValue

        Dim nID As Integer = 0
        Dim clientProps As IQueryable(Of cups_ClientPropertyLandlord)
        If (data.ID = 0) Then
            Return InsertClientProperty(clientID, data)
        End If
        clientProps = From s In _clientPropertyLandlordRepo.Query() _
                     Where s.ClientID = clientID And s.ID = data.ID _
                     Select s

        If clientProps.Count > 0 Then
            Dim cp As cups_ClientPropertyLandlord = clientProps.First()
            cp.PropertyLandlordID = data.PropertyID
            If (Not String.IsNullOrEmpty(data.FromDate)) Then
                DateTime.TryParse(data.FromDate, fromDate)
                cp.DateStart = fromDate
            End If
            If (Not String.IsNullOrEmpty(data.ToDate)) Then
                DateTime.TryParse(data.ToDate, toDate)
                cp.DateEnd = toDate
            End If
            cp.UpdatedBy = GetCurrentUser()
            cp.UpdatedOn = DateTime.Now
            nID = cp.ID
        Else
            Return InsertClientProperty(clientID, data)
        End If
        _clientPropertyLandlordRepo.SubmitChanges()

        Return nID

    End Function
    <RequiresAuthentication()> _
    Public Function InsertClientProperty(ByVal clientID As Integer, ByVal data As ClientProperty) As Integer
        Dim fromDate As DateTime = DateTime.MinValue
        Dim toDate As DateTime = DateTime.MinValue
        Dim cp As New cups_ClientPropertyLandlord
        cp.ClientID = clientID
        cp.DateStart = Nothing
        cp.DateEnd = Nothing
        If (Not String.IsNullOrEmpty(data.FromDate)) Then
            DateTime.TryParse(data.FromDate, fromDate)
            cp.DateStart = fromDate
        End If
        If (Not String.IsNullOrEmpty(data.ToDate)) Then
            DateTime.TryParse(data.ToDate, toDate)
            cp.DateEnd = toDate
        End If


        cp.PropertyLandlordID = data.PropertyID
        cp.UpdatedBy = GetCurrentUser()
        cp.UpdatedOn = DateTime.Now
        _clientPropertyLandlordRepo.Add(cp)
        _clientPropertyLandlordRepo.SubmitChanges()
        Return cp.ID
    End Function
    <RequiresAuthentication()> _
    Public Function GetPropertyByLandlord(ByVal landlordID As Integer) As IList(Of cups_PropertyLandlord)
        Dim qry = From p In _repoProperty.Query() _
                 Where p.LandlordID = landlordID _
                 Select p
        Return qry.ToList()

    End Function
    <RequiresAuthentication()> _
    Public Function GetPropertyByLandlord(ByVal landlordID As Integer, ByVal address As String) As IList(Of cups_PropertyLandlord)
        If (String.IsNullOrEmpty(address)) Then
            Return Nothing
        End If
        Dim qry = From p In _repoProperty.Query() _
                 Where p.LandlordID = landlordID And p.Address = address _
                 Select p
        Return qry.ToList()
    End Function
    <RequiresAuthentication()> _
    Public Function GetDistinctAddressByLandlord(ByVal landlordID As Integer) As IList(Of AddressProperty)
        Dim aList As New List(Of AddressProperty)
        Dim nIndex As Integer = 1
        Dim qry = From p In _repoProperty.Query() _
                  Where p.LandlordID = landlordID _
                  Select p.Address Distinct

        For Each q In qry
            Dim a As AddressProperty = New AddressProperty
            a.ID = nIndex
            a.Address = q
            aList.Add(a)
            nIndex = nIndex + 1
        Next
        Return aList
    End Function
    <RequiresAuthentication()> _
    Public Function SearchInterviewData(ByVal data As InterviewData, ByVal templateID As Integer, ByVal clientQry As IQueryable(Of cups_Client)) As IQueryable(Of cups_Client)
        Dim matchingClients As New List(Of cups_Client)
        Dim qry As IQueryable(Of cups_ControlMapping)
        Dim ctlIndex As Integer = 0

        Try
            Dim qryCtl As IQueryable(Of cups_ControlMapping) = From q In _controlMappingRepo.Query() _
                                                       Where q.cups_InterviewTemplateQuestion.InterviewTemplateID = templateID And q.IsAnswer = True _
                                                       Select q

            'Get interview question results that match

            For Each iView In data.AnswersText.Keys
                Dim controlID As String = iView
                If (Not String.IsNullOrEmpty(data.AnswersText(controlID))) Then
                    If (ctlIndex = 0) Then
                        qry = From q1 In qryCtl _
                                Where q1.ControlID = controlID _
                                Select q1
                    Else
                        qry = qry.Union(From q1 In qryCtl _
                                Where q1.ControlID = controlID _
                                Select q1)
                    End If
                    ctlIndex = ctlIndex + 1
                End If
            Next


            Dim qry1 = From iQ In _interviewQuestionRepo.Query() _
                       Group Join iC In qry On iQ.InterviewTemplateQuestionID Equals iC.InterviewQuestionTemplateID Into m = Group _
                        From mi In m _
                        Order By iQ.cups_Interview.cups_Client.ID _
                       Select iQ.cups_Interview.cups_Client, iQ.Answer, iQ.ControlIDs, mi.ControlID, mi.IsAnswer

            Dim isMatch = True
            Dim currentClient As cups_Client = qry1.First().cups_Client

            For Each e In qry1
                If (currentClient.ID <> e.cups_Client.ID) Then
                    currentClient = e.cups_Client
                    isMatch = True
                End If
                If (isMatch) Then


                    Dim nIndex = 0
                    Dim arrayAnswer(0) As String
                    Dim arrayControls(0) As String
                    If (Not String.IsNullOrEmpty(e.Answer)) Then
                        arrayAnswer = e.Answer.Split(",")
                    End If
                    If (Not String.IsNullOrEmpty(e.ControlIDs)) Then
                        arrayControls = e.ControlIDs.Split(",")
                    End If
                    If (arrayAnswer.Count() <> arrayControls.Count()) Then
                        If (data.AnswersText(e.ControlIDs) = e.Answer) Then
                            isMatch = isMatch And True
                        Else
                            isMatch = False
                        End If
                    Else
                        For Each e1 In arrayControls
                            If (data.AnswersText(e.ControlID) = arrayAnswer(nIndex)) Then
                                isMatch = isMatch And True
                            Else
                                isMatch = False
                            End If
                            nIndex = nIndex + 1
                        Next
                    End If
                End If
                If (isMatch) Then
                    matchingClients.Add(currentClient)
                End If
            Next


            Return matchingClients.AsQueryable().Intersect(clientQry)
        Catch ex As Exception
            'TODO: Log this
            Return clientQry
        End Try

        Return Nothing

    End Function


    <RequiresAuthentication()> _
    Public Function GetCurrentUser() As String
        Dim usr As String = "System"
        Try
            If (Not IsNothing(HttpContext.Current)) Then
                usr = HttpContext.Current.User.Identity.Name
            End If
        Catch ex As Exception

        End Try
        Return usr
    End Function
    <RequiresAuthentication()> _
    Public Function GetAllUsers(ByVal maximumRows As Integer, ByVal startRowIndex As Integer) As IList(Of MembershipUser)
        Dim memList As IList(Of MembershipUser) = New List(Of MembershipUser)
        Dim memCollection As MembershipUserCollection
        Dim selectCountValue = 0
        If (startRowIndex > 0) Then
            startRowIndex = startRowIndex / maximumRows
        End If

        If (maximumRows > 0) Then
            memCollection = Membership.GetAllUsers(startRowIndex, maximumRows, selectCountValue)
        Else
            memCollection = Membership.GetAllUsers()
        End If

        For Each i In memCollection
            memList.Add(i)
        Next

        Return memList
    End Function
    <RequiresAuthentication()> _
    Public Function CreateUser(ByVal name As String, ByVal password As String) As Boolean
        Dim member As MembershipUser = Membership.CreateUser(name, password)
        Return True
    End Function
    <RequiresAuthentication()> _
    Public Function ChangePassword(ByVal username As String, ByVal currentPassword As String, ByVal newPassword As String) As Boolean
        Dim isChanged = False
        Dim memColl As MembershipUserCollection = Membership.FindUsersByName(username)
        For Each m As MembershipUser In memColl
            isChanged = m.ChangePassword(currentPassword, newPassword)
        Next
        Return isChanged
    End Function
    <RequiresAuthentication()> _
    Public Function DeleteUser(ByVal Username As String) As Boolean
        Return Membership.DeleteUser(Username)

    End Function
    <RequiresAuthentication()> _
    Public Function ResetPassword(ByVal username As String) As String
        Dim strPwd As String = ""
        Dim memColl As MembershipUserCollection = Membership.FindUsersByName(username)
        For Each m As MembershipUser In memColl
            strPwd = m.ResetPassword()
        Next
        Return strPwd
    End Function
    <RequiresAuthentication()> _
    Public Function UnlockUser(ByVal username As String) As Boolean
        Dim isUnlocked As Boolean = False
        Dim memColl As MembershipUserCollection = Membership.FindUsersByName(username)
        For Each m As MembershipUser In memColl
            isUnlocked = m.UnlockUser()
        Next
        Return isUnlocked
    End Function
    <RequiresAuthentication()> _
    Public Function DisableClient(ByVal ID As Integer) As Boolean
        Try
            Dim client As cups_Client = GetClient(ID)
            client.Active = False
            _clientRepo.SubmitChanges()
            Return True
        Catch ex As Exception
            Return False
        End Try


    End Function
    <RequiresAuthentication()> _
    Public Function EnableClient(ByVal ID As Integer) As Boolean
        Try
            Dim client As cups_Client = GetClient(ID)
            client.Active = True
            _clientRepo.SubmitChanges()
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    <RequiresAuthentication()> _
    Public Function GetAllClients() As System.Linq.IQueryable(Of Data.cups_Client) Implements IClientDomainService.GetClients
        Dim clients As IQueryable(Of cups_Client) = From c In GetCups_Clients() _
                                                    Select c
        Return clients

    End Function
    <RequiresAuthentication()> _
    Public Function GetAllClientSummary(ByVal maximumRows As Integer, ByVal startRowIndex As Integer, ByVal sortType As String) As IList(Of ClientSummary) Implements IClientDomainService.GetClientSummary
        Dim clients As IQueryable

        Dim sortType1 As String
        If (sortType.Contains("LastUpdateDays")) Then
            sortType = sortType.Replace("LastUpdateDays", "DateStarted")
        End If
        If (sortType.Contains("CurrentStatus")) Then
            sortType1 = sortType
            sortType = ""
        End If
        If (maximumRows > 0) Then
            clients = From c In GetAllClients() _
                        .SortBy(sortType) _
                        .Skip(startRowIndex) _
                        .Take(maximumRows) _
                        Select New With {c.ID, c.LastName, c.FirstName, .CurrentStatus = c.cups_CurrentClientStatus.cups_StatusUpdateType.Name, c.DateStarted, c.cups_CurrentClientStatus.UpdatedOn}

        Else
            clients = From c In GetAllClients() _
                        .SortBy(sortType) _
                        .Skip(startRowIndex) _
                        Select New With {c.ID, c.LastName, c.FirstName, .CurrentStatus = c.cups_CurrentClientStatus.cups_StatusUpdateType.Name, c.DateStarted, c.cups_CurrentClientStatus.UpdatedOn}
        End If

        Dim cSummaries As New List(Of ClientSummary)

        Dim cSummary As ClientSummary

        For Each c In clients
            cSummary = New ClientSummary()
            cSummary.ID = c.ID
            cSummary.FirstName = c.FirstName
            cSummary.LastName = c.LastName
            cSummary.CurrentStatus = c.CurrentStatus
            cSummary.DateStarted = c.DateStarted
            If (IsNothing(c.UpdatedOn)) Then
                cSummary.LastUpdateDays = 0
            Else
                cSummary.LastUpdateDays = DateTime.Now.Subtract(c.UpdatedOn).Days
            End If
            cSummaries.Add(cSummary)
        Next

        Return cSummaries.ToList()
    End Function

    <RequiresAuthentication()> _
    Public Function DeleteClientStatus(ByVal id As Integer) As Boolean
        Dim csStatus As New ClientStatus
        Try
            Dim cs = (From ct In _statusRepo.Query() _
                 Where ct.ID = id _
                 Select ct).FirstOrDefault()
            If (Not IsNothing(cs)) Then
                _statusRepo.Remove(cs)
                Return True
            End If
        Catch e As Exception
            'TODO: log this
        End Try
        Return False
    End Function
End Class

